package com.njsoft.bodyawakening.table.fragment;


import android.graphics.Rect;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;

import com.njsoft.bodyawakening.BodyApplication;
import com.njsoft.bodyawakening.R;
import com.njsoft.bodyawakening.event.MeaEventBus;
import com.njsoft.bodyawakening.table.adapter.TableAdapter;
import com.njsoft.bodyawakening.utils.APPInfo;
import com.njsoft.bodyawakening.utils.L;
import com.njsoft.bodyawakening.utils.TimeUtil;
import com.njsoft.bodyawakening.view.DragView;
import com.njsoft.bodyawakening.view.GridRelativeLayout;
import com.njsoft.bodyawakening.view.MyToast;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link TableFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class TableFragment extends Fragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    @BindView(R.id.mTableList)
    RecyclerView mTableList;
    Unbinder unbinder;
    @BindView(R.id.MyGrid)
    GridRelativeLayout myGridView;
    private int width;
    private int height;
    private TableAdapter tableAdapter;
    private String TAG = "table-->";
    private boolean isFirstMeasure = true;

    public TableFragment() {
    }


    public static TableFragment newInstance(int width, int height) {
        TableFragment fragment = new TableFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_PARAM1, width);
        args.putInt(ARG_PARAM2, height);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_table, container, false);
        unbinder = ButterKnife.bind(this, view);
        EventBus.getDefault().register(this);
        MeasureGridView();
        initLine();
        return view;
    }

    /**
     * 测量视图的高度，宽度
     */
    private void MeasureGridView() {
        myGridView.getViewTreeObserver().addOnPreDrawListener(
                new ViewTreeObserver.OnPreDrawListener() {

                    @Override
                    public boolean onPreDraw() {
                        myGridView.getViewTreeObserver().removeOnPreDrawListener(this);
                        BodyApplication.GRIDVIEW_WIDTH = myGridView.getWidth(); // 获取宽度
                        BodyApplication.GRIDVIEW_HEIGHT = myGridView.getHeight(); // 获取高度
                        if (isFirstMeasure) {
                            isFirstMeasure = false;
                            EventBus.getDefault().post(new MeaEventBus());
                        }
                        return true;
                    }
                });
    }

    private void initLine() {
        ArrayList<String> timeList = TimeUtil.getListFormATimetoBtime(
                getString(R.string.BeginTime)
                , getString(R.string.EndTime),
                60 * 60 * 1000);
        int Hnum = timeList.size();
        if (getArguments() != null) {
            width = getArguments().getInt(ARG_PARAM1);
            height = getArguments().getInt(ARG_PARAM2);
        }
        myGridView.setInf(8, Hnum, APPInfo.getWidth(getActivity()) - width, height);
        myGridView.setClickListener(new GridRelativeLayout.MyItemClickListener() {
            @Override
            public void onClickPoint(String time) {
                MyToast.show(getContext(), time);
            }
        });
    }

    private void addView() {
        DragView dragView = new DragView(getContext());
        dragView.setBackgroundColor(getActivity().getResources().getColor(R.color.red));
        dragView.setWidth(BodyApplication.GRIDVIEW_WIDTH / 7);
        dragView.setHeight(BodyApplication.GRIDVIEW_WIDTH / 7*4);

        dragView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyToast.show(getContext(), "点我!");
            }
        });
        dragView.setText("你好!");
        Rect rect=new Rect();
        myGridView.getWindowVisibleDisplayFrame(rect);
        BodyApplication.GRIDVIEW_visibility_Height=rect.height();
        myGridView.addView(dragView);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        EventBus.getDefault().unregister(this);
    }

    private boolean isFirstEvent=true;
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void DrawRecView(MeaEventBus meaEventBus) {
        if (isFirstEvent) {
            isFirstEvent=false;
            addView();
        }
    }
}
